package cn.sirun.com.fca.account.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.CarBelongtoAuthLoader;
import cn.sirun.com.fca.account.loader.PhoneLoader;
import cn.sirun.com.fca.account.model.base.CarInfoDetailRequest;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.base.SubmitPhoneRequest;
import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.account.model.bind.SubmitCarAuthenticationRequest;
import cn.sirun.com.fca.account.model.bind.SubmitCarAuthenticationResponse;
import cn.sirun.com.fca.account.model.bind.SubmitPersonalInfoRequest;
import cn.sirun.com.fca.account.model.bind.SubmitPersonalInfoResponse;
import cn.sirun.com.fca.account.model.register.SubmitEmailRequest;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.ICarBelongtoAuthPresenter;
import cn.sirun.com.fca.account.presenter.IPhonePresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class CarBelongtoAuthPresenter extends AccountFlowPresenter<CarBelongtoAuthLoader> implements ICarBelongtoAuthPresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    public void getCarInfoDetail(CarInfoDetailRequest request) {
        getLoader().getCarInfoDetail(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<CarInfoDetailResponse>>() {

                    @Override
                    public void onNext(BaseResponse<CarInfoDetailResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        CarBelongtoAuthPresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_GETCARINFODETAIL_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }


    @Override
    public void submitPersonalInfo(SubmitPersonalInfoRequest request) {
        getLoader().submitPersonalInfo(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPersonalInfoResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPersonalInfoResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        CarBelongtoAuthPresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_SUBMITPERSONINFO_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }


    @Override
    public void submitCarAuthentication(SubmitCarAuthenticationRequest request) {
        getLoader().submitCarAuthentication(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitCarAuthenticationResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitCarAuthenticationResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        CarBelongtoAuthPresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_SUBMIT_CAR_AUTHENTICATION_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }



    @Override
    protected CarBelongtoAuthLoader createLoader() {
        return new CarBelongtoAuthLoader();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
